package p.sglmsn.top.ext.wx.wxext.http;

import cn.hutool.json.JSONObject;
import cn.hutool.setting.Setting;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import p.sglmsn.top.ext.wx.entity.ComeBack;

import java.net.URLEncoder;

@Slf4j
@Component
public class HttpUtil {

    public static  String mac;
    /**
     * 微信启动路径路径
     */
    public static Setting setting = new Setting("setting.setting");
    public static String path=setting.getStr("path",null);

    static {
        try {
            mac = URLEncoder.encode(getMac());
        } catch (Exception e) {
            log.error("无法加载mac");
            mac = "错误的";
        }
    }


    public static String getUrl() {
        return setting.getStr("key");
    }

    public static ComeBack send(SendDto sendDto) {
        if (log.isDebugEnabled()) {
            log.debug("请求:{}", sendDto);
        }
        String post = cn.hutool.http.HttpUtil.post(getUrl(), new JSONObject(sendDto), 10000);
        if (log.isDebugEnabled()) {
            log.debug("响应:{}", post);
        }
        return JSON.parseObject(post, ComeBack.class);
    }

    private static String getMac() {
        ComeBack mac = send(SendDto.create("mac"));
        return mac.getData().toString();

    }

}
